package com.skeleton.show.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.skeleton.common.base.service.impl.BaseServiceImpl;
import com.skeleton.db.dao.IShowinfoDao;
import com.skeleton.db.dto.ShowinfoDto;
import com.skeleton.db.entity.Showinfo;
import com.skeleton.show.service.ShowService;

/**
 * Service实现
 * 
 * @author Wangyu
 */
@Service
public class ShowServiceImpl extends BaseServiceImpl implements ShowService {

	@Resource
	private IShowinfoDao iShowinfoDao;

	@Override
	public List<ShowinfoDto> getAllShow() {
		logger.info("========ShowServiceImpl=============getAllShow========");
		List<Showinfo> sList = iShowinfoDao.selectAll();
		List<ShowinfoDto> sdList = new ArrayList<ShowinfoDto>();

		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日HH时mm分");

		for (int i = 0; i < sList.size(); i++) {
			ShowinfoDto sd = sList.get(i).toDto();
			if (sd.getFinishDttm() != null) {
				sd.setFinishDttmStr(sdf.format(sd.getFinishDttm()));
			}
			if (sd.getEndDttm() != null) {
				sd.setEndDttmStr(sdf.format(sd.getEndDttm()));
			}
			if (sd.getStartDttm() != null) {
				sd.setStartDttmStr(sdf.format(sd.getStartDttm()));
			}
			sdList.add(sd);
		}
		return sdList;
	}

	@Override
	public void doAddMain(ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============doAddMain========");

		int id = 0;
		Integer maxId = iShowinfoDao.selectMaxId("___");
		logger.info("maxId" + maxId);

		if (maxId == null) {
			id = 1;
		} else {
			id = ++maxId;
		}
		String idStr = id + "";
		if (id < 100) {
			idStr = "0" + id;
		}
		if (id < 10) {
			idStr = "0" + idStr;
		}

		Showinfo sEntity = sDto.toEntity();
		sEntity.setShow_id(idStr);

		iShowinfoDao.insert(sEntity);
	}

	@Override
	public void doAddSub(String mainId, ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============doAddSub========");

		int id = 0;
		Integer maxId = iShowinfoDao.selectMaxId(mainId + "___");
		logger.info("maxId" + maxId);

		if (maxId == null) {
			id = Integer.parseInt(mainId + "001");
		} else {
			id = ++maxId;
		}

		String idStr = id + "";
		if (id < 100000) {
			idStr = "0" + id;
		}
		if (id < 10000) {
			idStr = "0" + idStr;
		}

		Showinfo sEntity = sDto.toEntity();
		sEntity.setShow_id(idStr);

		iShowinfoDao.insert(sEntity);
	}

	@Override
	public void doAddItem(String subId, ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============doAddItem========");

		int id = 0;
		Integer maxId = iShowinfoDao.selectMaxId(subId + "___");
		logger.info("maxId" + maxId);

		if (maxId == null) {
			id = Integer.parseInt(subId + "001");
		} else {
			id = ++maxId;
		}
		String idStr = id + "";
		if (id < 100000000) {
			idStr = "0" + id;
		}
		if (id < 10000000) {
			idStr = "0" + idStr;
		}

		Showinfo sEntity = sDto.toEntity();
		sEntity.setShow_id(idStr);

		iShowinfoDao.insert(sEntity);
	}

	@Override
	public ShowinfoDto getItemById(ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============getItemById========");
		Showinfo sEntity = sDto.toEntity();
		Showinfo s = iShowinfoDao.selectOneById(sEntity);
		ShowinfoDto sd = s.toDto();
		return sd;
	}

	@Override
	public void doModifyItem(ShowinfoDto sDto, String isUpdate) {
		logger.info("========ShowServiceImpl=============doModifyItem========");

		Showinfo sEntity = sDto.toEntity();

		if (sEntity.getShow_status() == '1' && isUpdate != null) {
			sEntity.setFinish_dttm(new Date());
			iShowinfoDao.update(sEntity);
		} else {
			iShowinfoDao.updateWithOutFinishTime(sEntity);
		}

	}

	@Override
	public void doDeleteItem(ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============doDeleteItem========");
		Showinfo sEntity = sDto.toEntity();
		iShowinfoDao.delete(sEntity);
	}

	@Override
	public void doDeleteSub(ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============doDeleteSub========");
		Showinfo sEntity = sDto.toEntity();
		iShowinfoDao.deleteSub(sEntity.getShow_id(), sEntity.getShow_id() + "___");
	}

	@Override
	public void doDeleteMain(ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============doDeleteMain========");
		Showinfo sEntity = sDto.toEntity();
		iShowinfoDao.deleteMain(sEntity.getShow_id(), sEntity.getShow_id() + "___", sEntity.getShow_id() + "______");
	}
	
	@Override
	public void doDeleteAllDate(ShowinfoDto sDto) {
		logger.info("========ShowServiceImpl=============doDeleteMain========");
		Showinfo sEntity = sDto.toEntity();
		iShowinfoDao.deleteAllDate(sEntity.getShow_id() + "______");
	}

}
